package NewTmas;

import com.mxgraph.swing.mxGraphComponent;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/22/11
 * Time: 1:35 PM
 * This class is used for printing JGraph generated graphs
 */
public class PrintJGraphMenuItem extends JMenuItem  {
    mxGraphComponent graphComponent1;



    public PrintJGraphMenuItem(final mxGraphComponent graphComponent,String jobName){

        this.graphComponent1=graphComponent;


        setText(jobName);
        setVisible(true);

        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                PrinterJob pj = PrinterJob.getPrinterJob();

                if (pj.printDialog())
                {
                    PageFormat pf = graphComponent.getPageFormat();
                    Paper paper = new Paper();
                    double margin = 36;
                    paper.setImageableArea(margin, margin, paper.getWidth()
                            - margin * 2, paper.getHeight() - margin * 2);
                    pf.setPaper(paper);
                    pj.setPrintable(graphComponent, pf);

                    try
                    {
                        pj.print();
                    }
                    catch (PrinterException e2)
                    {
                          JOptionPane.showMessageDialog(null, "Problem printing the graph: " + e2.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });



    }





}


